package com.ythook.mentor.controller;

import com.ythook.mentor.entity.*;
import com.ythook.mentor.service.UserService;
import com.ythook.mentor.utils.CodeUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/api")
public class UserController {
    @Resource
    private UserService userService;
    private Logger logger = LoggerFactory.getLogger(UserController.class);

    @GetMapping("/users")
    public ResponseEntity<List<User>> search() {
        return ResponseEntity.ok(userService.search());
    }

    @GetMapping("/user/{id}")
    public ResponseEntity<User> search(@PathVariable("id") Long id) {
        return ResponseEntity.ok(userService.findById(id).orElse(new User()));
    }

    @PostMapping("/user/searchMentorByKey")
//    public ResponseEntity<List<User>> searchMentorByKey(@RequestParam(value = "startDate",required = false) Date startDate,
//                                                          @RequestParam(value = "endDate",required = false) Date endDate,
//                                                          @RequestParam(value = "key",required = false) String key) {
    public ResponseEntity<Object> searchMentorByKey(@RequestBody SearchMentorVo searchMentorVo){
        Map<String,Object> map = new HashMap<>();
        if(searchMentorVo.getStartDate().after(searchMentorVo.getEndDate())){
            map.put("status","error");
            map.put("msg","Start date must be earlier than end date");
        }else{
            map.put("status","success");
            map.put("msg","search mentor successfully");
            map.put("data",userService.searchMentorByKey(searchMentorVo.getStartDate(), searchMentorVo.getEndDate(), searchMentorVo.getKey()));
        }
        return ResponseEntity.ok(map);
    }

    @DeleteMapping("/user/{id}")
    public ResponseEntity<Void> delete(@PathVariable("id") Long id){
        userService.delete(id);
        return ResponseEntity.ok().build();
    }

    @PostMapping("/user/add")
    public ResponseEntity<HttpStatus> add(@RequestBody User user){
        userService.add(user);
        return ResponseEntity.ok(HttpStatus.CREATED);
    }

    @PutMapping("/user/update")
    public ResponseEntity<Object> update(@RequestBody RegisterVo registerVo, HttpServletRequest request){
        Map<String,Object> map = new HashMap<>();
        if (!CodeUtil.checkCaptcha(request, registerVo.getCaptcha())) {
            map.put("status","codefail");
            return ResponseEntity.ok(map);
        }
        User user = new User();
        BeanUtils.copyProperties(registerVo, user);
        map.put("status","success");
        map.put("data",userService.update(user));
        return ResponseEntity.ok(map);
    }


    @PostMapping("/user/register")
    public @ResponseBody ResponseEntity<Object> register(@RequestBody RegisterVo registerVo, HttpServletRequest request){
        Map<String,Object> map = new HashMap<>();
        if (!CodeUtil.checkCaptcha(request, registerVo.getCaptcha())) {
            map.put("status","codefail");
            return ResponseEntity.ok(map);
        }
        map.put("status","success");
        map.put("data",userService.register(registerVo));
        return ResponseEntity.ok(map);
    }

    @PostMapping("/user/login")
    public @ResponseBody ResponseEntity<Object> login(@RequestBody LoginVo loginVo, HttpServletRequest request){
        Map<String,Object> map = new HashMap<>();
        if (!CodeUtil.checkCaptcha(request, loginVo.getCaptcha())) {
            map.put("status","codefail");
            return ResponseEntity.ok(map);
        }
        User user = userService.login(loginVo.getUsername(), loginVo.getPassword());
        if(user != null){
            map.put("status","success");
            map.put("data",user);
            logger.info("Login success - ", user.getUsername());
        }else{
            map.put("status","notfound");
            logger.info("Login fail");
        }
        return ResponseEntity.ok(map);
    }
}
